package com.example.xyr.myapplication.utils;

import android.graphics.Bitmap;
import android.graphics.Matrix;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * Created by ylcf on 2018/8/16.
 */

public class BitmapUtils {
    private static BitmapUtils bitmapUtils;

    private Bitmap mAvatar;

    private Bitmap oAvatar;
    private UserInfo myInfo;

    private BitmapUtils() {
        myInfo = JMessageClient.getMyInfo();
        myInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int i, String s, Bitmap bitmap) {
                if (i == 0) {
                    mAvatar = bitmap;
                }
            }
        });
    }

    public void upDateUserInfo() {
        myInfo = JMessageClient.getMyInfo();
        myInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int i, String s, Bitmap bitmap) {
                if (i == 0) {
                    mAvatar = bitmap;
                }
            }
        });
    }

    public static BitmapUtils getInstance() {
        if (bitmapUtils == null) {
            bitmapUtils = new BitmapUtils();
        }
        return bitmapUtils;
    }

    public String getUserId() {
        if (myInfo != null) {
            return myInfo.getUserName();
        } else {
            return "";
        }
    }


    public String getmNickName() {
        if (myInfo != null) {
            return myInfo.getNickname();
        } else {
            return "";
        }
    }

    public UserInfo.Gender getGender(){
        return myInfo.getGender();
    }

    public String getName() {
        if (myInfo != null) {
            return myInfo.getUserName();
        } else {
            return "";
        }
    }

    public Bitmap getmAvatar() {
        return mAvatar;
    }

    public void setmAvatar(Bitmap mAvatar) {
        this.mAvatar = mAvatar;
    }

    public Bitmap getoAvatar() {
        return oAvatar;
    }

    public void setoAvatar(Bitmap oAvatar) {
        this.oAvatar = oAvatar;
    }

    /**
     * 根据给定的宽和高进行拉伸
     *
     * @param origin    原图
     * @param newWidth  新图的宽
     * @param newHeight 新图的高
     * @return new Bitmap
     */
    public static Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
        if (origin == null) {
            return null;
        }
        int height = origin.getHeight();
        int width = origin.getWidth();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);// 使用后乘
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
//        if (!origin.isRecycled()) {
//            origin.recycle();
//        }
        return newBM;
    }

    public void onDestroy() {
        if (mAvatar != null) {
            mAvatar.recycle();
            mAvatar = null;
        }
        if (oAvatar != null) {
            oAvatar.recycle();
            oAvatar = null;
        }
    }
}
